package com.xiaohe66.web.application.love;

import com.xiaohe66.common.util.IdWorker;
import com.xiaohe66.common.web.sec.annotation.NeedRoleAll;
import com.xiaohe66.web.application.love.bo.MessageListBo;
import com.xiaohe66.web.application.love.bo.MessageSaveBo;
import com.xiaohe66.web.application.love.convert.LeaveWordBoConverter;
import com.xiaohe66.web.application.love.result.MessageListResult;
import com.xiaohe66.web.domain.account.value.AccountId;
import com.xiaohe66.web.domain.love.agg.LeaveWord;
import com.xiaohe66.web.domain.love.repository.LeaveWordRepository;
import com.xiaohe66.web.domain.love.service.LeaveWordService;
import com.xiaohe66.web.domain.love.value.LoverId;
import com.xiaohe66.web.domain.love.value.LeaveWordId;
import com.xiaohe66.web.domain.love.value.LeaveWordText;
import com.xiaohe66.web.domain.sys.sec.service.SecurityService;
import com.xiaohe66.web.domain.sys.sec.value.RoleName;
import com.xiaohe66.web.domain.wx.user.repository.WxUserRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author xiaohe
 * @since 2021.12.07 11:53
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class LeaveWordAppService {

    private final LeaveWordBoConverter boConverter;
    private final LeaveWordRepository leaveWordRepository;
    private final LeaveWordService leaveWordService;

    private final WxUserRepository wxUserRepository;
    private final SecurityService securityService;

    @NeedRoleAll(RoleName.LOVE_ROLE_VALUE)
    public MessageListResult save(MessageSaveBo bo) {

        AccountId currentAccountId = securityService.getCurrentAccountId();

        long id = IdWorker.genId();

        LeaveWord leaveWord = LeaveWord.builder()
                .id(new LeaveWordId(id))
                .createId(currentAccountId)
                .loverId(new LoverId(bo.getLoverId()))
                .text(new LeaveWordText(bo.getText()))
                .build();

        leaveWordService.save(leaveWord);

        return boConverter.toResult(wxUserRepository, leaveWord, currentAccountId);
    }

    @NeedRoleAll(RoleName.LOVE_ROLE_VALUE)
    public void remove(Long idValue) {

        LeaveWordId id = new LeaveWordId(idValue);
        leaveWordService.remove(id);
    }

    @NeedRoleAll(RoleName.LOVE_ROLE_VALUE)
    public List<MessageListResult> list(MessageListBo bo) {

        AccountId currentAccountId = securityService.getCurrentAccountId();

        LoverId loverId = new LoverId(bo.getLoverId());

        List<LeaveWord> leaveWordList = leaveWordService.listDesc(loverId, bo.toPaging());

        return boConverter.toResult(wxUserRepository, leaveWordList, currentAccountId);
    }
}
